package com.zxgj.datadirect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxgj.datadirect.entity.DataDirectOrg;
import com.zxgj.datadirect.mapper.DataDirectOrgMapper;
import com.zxgj.datadirect.service.DataDirectOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 机构信息服务实现类
 * 
 * @author ruoyi
 */
@Slf4j
@Service
public class DataDirectOrgServiceImpl extends ServiceImpl<DataDirectOrgMapper, DataDirectOrg> implements DataDirectOrgService {

    @Autowired
    private DataDirectOrgMapper orgMapper;

    @Override
    public List<DataDirectOrg> getOrgList(DataDirectOrg org) {
        QueryWrapper<DataDirectOrg> wrapper = new QueryWrapper<>();
        if (org.getOrgCode() != null && !org.getOrgCode().isEmpty()) {
            wrapper.like("org_code", org.getOrgCode());
        }
        if (org.getOrgName() != null && !org.getOrgName().isEmpty()) {
            wrapper.like("org_name", org.getOrgName());
        }
        if (org.getOrgLevel() != null && !org.getOrgLevel().isEmpty()) {
            wrapper.eq("org_level", org.getOrgLevel());
        }
        if (org.getStatus() != null && !org.getStatus().isEmpty()) {
            wrapper.eq("status", org.getStatus());
        }
        return list(wrapper);
    }

    @Override
    public Page<DataDirectOrg> getOrgPage(Page<DataDirectOrg> page, DataDirectOrg org) {
        QueryWrapper<DataDirectOrg> wrapper = new QueryWrapper<>();
        if (org != null) {
            if (org.getOrgCode() != null && !org.getOrgCode().isEmpty()) {
                wrapper.like("org_code", org.getOrgCode());
            }
            if (org.getOrgName() != null && !org.getOrgName().isEmpty()) {
                wrapper.like("org_name", org.getOrgName());
            }
            if (org.getOrgLevel() != null && !org.getOrgLevel().isEmpty()) {
                wrapper.eq("org_level", org.getOrgLevel());
            }
            if (org.getStatus() != null && !org.getStatus().isEmpty()) {
                wrapper.eq("status", org.getStatus());
            }
        }
        wrapper.orderByDesc("create_time");
        return this.page(page, wrapper);
    }

    @Override
    public DataDirectOrg getById(Long id) {
        return super.getById(id);
    }

    @Override
    public DataDirectOrg getByOrgCode(String orgCode) {
        QueryWrapper<DataDirectOrg> wrapper = new QueryWrapper<>();
        wrapper.eq("org_code", orgCode);
        return getOne(wrapper);
    }

    @Override
    public boolean addOrg(DataDirectOrg org) {
        return save(org);
    }

    @Override
    public boolean updateOrg(DataDirectOrg org) {
        return updateById(org);
    }

    @Override
    public boolean deleteOrg(Long id) {
        return removeById(id);
    }

    @Override
    public boolean deleteOrgBatch(Long[] ids) {
        return removeByIds(java.util.Arrays.asList(ids));
    }

    @Override
    public List<DataDirectOrg> getOrgStats() {
        // 返回机构统计信息
        List<DataDirectOrg> stats = new java.util.ArrayList<>();
        
        // 总机构数
        long totalCount = count();
        DataDirectOrg totalStat = new DataDirectOrg();
        totalStat.setOrgName("机构总数");
        totalStat.setId(Long.valueOf(totalCount));
        stats.add(totalStat);
        
        // 省级机构数
        QueryWrapper<DataDirectOrg> provinceWrapper = new QueryWrapper<>();
        provinceWrapper.eq("org_level", "PROVINCE");
        long provinceCount = count(provinceWrapper);
        DataDirectOrg provinceStat = new DataDirectOrg();
        provinceStat.setOrgName("省级机构");
        provinceStat.setId(Long.valueOf(provinceCount));
        stats.add(provinceStat);
        
        // 市级机构数
        QueryWrapper<DataDirectOrg> cityWrapper = new QueryWrapper<>();
        cityWrapper.eq("org_level", "CITY");
        long cityCount = count(cityWrapper);
        DataDirectOrg cityStat = new DataDirectOrg();
        cityStat.setOrgName("市级机构");
        cityStat.setId(Long.valueOf(cityCount));
        stats.add(cityStat);
        
        // 县级机构数
        QueryWrapper<DataDirectOrg> countyWrapper = new QueryWrapper<>();
        countyWrapper.eq("org_level", "COUNTY");
        long countyCount = count(countyWrapper);
        DataDirectOrg countyStat = new DataDirectOrg();
        countyStat.setOrgName("县级机构");
        countyStat.setId(Long.valueOf(countyCount));
        stats.add(countyStat);
        
        return stats;
    }

    @Override
    public List<DataDirectOrg> getParentOrgOptions() {
        QueryWrapper<DataDirectOrg> wrapper = new QueryWrapper<>();
        wrapper.select("id", "org_code", "org_name", "org_level");
        wrapper.eq("status", "NORMAL");
        wrapper.orderByAsc("org_level", "org_name");
        return list(wrapper);
    }

    @Override
    public int syncFromNationalPlatform() {
        log.info("开始同步国家平台机构数据");
        try {
            // 这里实现具体的同步逻辑
            // 暂时返回0，表示没有同步数据
            log.info("机构数据同步完成");
            return 0;
        } catch (Exception e) {
            log.error("机构数据同步失败: {}", e.getMessage(), e);
            return 0;
        }
    }
}
